import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;


public class UdpServer {
	
	public static void main(String[] args) {
		try {
			System.out.println("start");
			DatagramSocket datagramSocket = new DatagramSocket(8888);
			byte buf[]= new byte[1024];
			DatagramPacket datagramPacket = new DatagramPacket(buf, buf.length);
			
			datagramSocket.receive(datagramPacket);
			InetAddress inetAddress = datagramPacket.getAddress();
			int port = datagramPacket.getPort();
			System.out.println(inetAddress.getHostName().toString()+":"+port);
			
			
			
			
			datagramSocket.receive(datagramPacket);
			InetAddress inetAddressTemp = datagramPacket.getAddress();
			int portTemp = datagramPacket.getPort();
			System.out.println(inetAddressTemp.getHostName().toString()+":"+portTemp);
			
			
			String data = inetAddress.getHostName()+":"+port;
			System.out.println(data+"==="+inetAddressTemp.getHostName()+":"+portTemp);
			datagramPacket = new DatagramPacket(data.getBytes(), data.getBytes().length, inetAddressTemp, portTemp);
			datagramSocket.send(datagramPacket);
			
			data = inetAddressTemp.getHostName()+":"+portTemp;
			System.out.println(data+"==="+inetAddress.getHostName()+":"+port);
			datagramPacket = new DatagramPacket(data.getBytes(), data.getBytes().length, inetAddress, port);
			datagramSocket.send(datagramPacket);
			
		} catch (SocketException e) {
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

}
